package tpase.StorageServer.Controller;

import tpase.StorageServer.Handler.StorageServerStartHandler;
import tpase.StorageServer.Service.Impl.StorageServerServiceImpl;
import tpase.StorageServer.Service.StorageServerService;
import tpase.StorageServer.VO.Address;
import util.Util;

public class StorageServerController {
    private boolean isDebug;
    private boolean isSetup;
    private String paramPath;
    private Address address;
    private Address setupAddress;
    private StorageServerService storageServerService = new StorageServerServiceImpl();
    public StorageServerController(String paramPath, boolean isSetup, boolean isDebug) {
        this.paramPath = paramPath;
        this.isDebug = isDebug;
        this.isSetup = isSetup;
        loadParameters();
        if (isSetup == false) {
            storageServerService.clear();
        }
    }

    public void setup() {
    }

    public void start() {
        new Thread(() -> {
            Util.processServerBootstrap(new StorageServerStartHandler(storageServerService, paramPath), address.getPort());
        }).start();
    }


    private void loadParameters() {
        String paramName = "/parameters.json";
        String setupName = "/setupParameters.json";
        address = Util.jsonFileToObj(paramPath + paramName, Address.class);
        setupAddress = Util.jsonFileToObj(paramPath + setupName, Address.class);
    }
}
